﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.IO;
using Valentia.Kinection.Remoting.Helper;
using Valentia.Kinection.DataTypes;

namespace Valentia.Kinection.Remoting.ClientTest
{
    public partial class Form1 : Form
    {
        #region Declarations

        private const String BREAK_LINE = "\r\n";

        private Client remotingClient = new Client(null, null, null, null, -1, -1);

        #endregion

        #region Constructor

        public Form1()
        {
            InitializeComponent();
        }

        #endregion

        #region Form Control Event Handlers

        private void btnSendDeviceConnectionInfo_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtCommand.Text.Trim().Equals("1"))
                {
                    remotingClient.SendCommand(ClientCommands.ClientConnected);
                }

                if (txtCommand.Text.Trim().Equals("2"))
                {
                    remotingClient.SendCommand(ClientCommands.SendClientsConnectivity);
                }

                if (txtCommand.Text.Trim().Equals("3"))
                {
                    remotingClient.SendCommand(ClientCommands.SendConnectivityStatus);
                }

                if (txtCommand.Text.Trim().Equals("4"))
                {
                    remotingClient.SendCommand(ClientCommands.SendDevicesConnectivity);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                remotingClient.Connect();

                remotingClient.DeviceConnectivityInfoEvent
                    += new Client.DeviceConnectivityInfoEventDlg(remotingClient_DeviceConnectivityInfoEvent);

                remotingClient.ClientConnectivityInfoEvent
                    += new Client.ClientConnectivityInfoEventDlg(remotingClient_ClientConnectivityInfoEvent);

                remotingClient.ConnectionStatusInfoEvent
                    += new Client.ConnectionStatusEventDlg(remotingClient_ConnectionStatusInfoEvent);

                remotingClient.ServerConnectionStatus
                    += new EventHandler(remotingClient_ServerConnectionStatus);

                remotingClient.ShowNotificationReceived
                    += new EventHandler<NotificationReceivedArgs>(remotingClient_ShowNotificationReceived);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

      

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                remotingClient.Disconnect();

                remotingClient.DeviceConnectivityInfoEvent
                    -= new Client.DeviceConnectivityInfoEventDlg(remotingClient_DeviceConnectivityInfoEvent);

                remotingClient.ClientConnectivityInfoEvent
                    -= new Client.ClientConnectivityInfoEventDlg(remotingClient_ClientConnectivityInfoEvent);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Event Handlers

        void remotingClient_DeviceConnectivityInfoEvent(List<KinectDeviceInfo> deviceConnectionInfo, String errorMessage)
        {
            try
            {
                MethodInvoker UpdateSubItem = delegate
                {
                    txtDeviceClientInfo.Text = string.Empty;

                    txtDeviceClientInfo.Text += DateTime.Now.ToString("s")
                  + " >> remotingClient_DeviceConnectivityInfoEvent() Invoked! "
                  + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem);


                if (deviceConnectionInfo == null)
                {
                    return;
                }

                MethodInvoker UpdateSubItem1 = delegate
                {
                    txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Device Connection List Count >> "
                     + deviceConnectionInfo.Count + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem1);


                foreach (KinectDeviceInfo deviceConnInfo in deviceConnectionInfo)
                {
                    MethodInvoker UpdateSubItem2 = delegate
                    {
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Device ID >> "
                       + deviceConnInfo.DeviceID + BREAK_LINE;
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Device Name >> "
                            + deviceConnInfo.DeviceName + BREAK_LINE;
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Device Connection >> "
                            + deviceConnInfo.KinectDeviceStatus + BREAK_LINE;
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Device Locked >> "
                            + deviceConnInfo.DeviceLocked + BREAK_LINE;
                    };
                    this.Invoke(UpdateSubItem2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void remotingClient_ClientConnectivityInfoEvent(List<ServiceClientInfo> clientConnectionInfo, String errorMessage)
        {
            try
            {
                MethodInvoker UpdateSubItem = delegate
                {
                    txtDeviceClientInfo.Text = string.Empty;

                    txtDeviceClientInfo.Text += DateTime.Now.ToString("s")
                  + " >> remotingClient_ClientConnectivityInfoEvent() Invoked! "
                  + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem);

                if (clientConnectionInfo == null)
                {
                    return;
                }

                MethodInvoker UpdateSubItem1 = delegate
                {
                    txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Client Connection List Count >> "
                     + clientConnectionInfo.Count + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem1);


                foreach (ServiceClientInfo deviceConnInfo in clientConnectionInfo)
                {
                    MethodInvoker UpdateSubItem2 = delegate
                    {
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Client ID >> "
                       + deviceConnInfo.ClientID + BREAK_LINE;
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Client Name >> "
                            + deviceConnInfo.ClientName + BREAK_LINE;
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Client Type >> "
                            + deviceConnInfo.ClientType + BREAK_LINE;
                        txtDeviceClientInfo.Text += DateTime.Now.ToString("s") + " >> Client Connected >> "
                            + deviceConnInfo.Connected + BREAK_LINE;
                    };
                    this.Invoke(UpdateSubItem2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void remotingClient_ServerConnectionStatus(object sender, EventArgs e)
        {
            try
            {
                if (e == null)
                {
                    MethodInvoker UpdateSubItem = delegate
                    {
                        txtActivity.Text += DateTime.Now.ToString("s")
                      + " >> remotingClient_ServerConnectionStatus() Invoked! Server Disconnected "
                      + BREAK_LINE;
                    };
                    this.Invoke(UpdateSubItem);
                }
                else
                {
                    MethodInvoker UpdateSubItem = delegate
                    {
                     

                        txtActivity.Text += DateTime.Now.ToString("s")
                      + " >> remotingClient_ServerConnectionStatus() Invoked! Server is Connected"
                      + BREAK_LINE;
                    };
                    this.Invoke(UpdateSubItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }

        void remotingClient_ConnectionStatusInfoEvent(ConnectivityStatus connectivityStatus, int connectedDeviceCount, int connectedClientCount, String errorMessage)
        {
            try
            {
                MethodInvoker UpdateSubItem = delegate
                {
                    txtActivity.Text += DateTime.Now.ToString("s")
                  + " >> remotingClient_ConnectionStatusInfoEvent() Invoked! Connectivity status "
                  + connectivityStatus + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void remotingClient_ShowNotificationReceived(object sender, NotificationReceivedArgs e)
        {
            try
            {
                MethodInvoker UpdateSubItem = delegate
                {
                    txtActivity.Text += DateTime.Now.ToString("s")
                  + " >> remotingClient_ShowNotificationReceived() Invoked! Message "
                  + e.Message + BREAK_LINE;
                };
                this.Invoke(UpdateSubItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        #endregion


    }
}
